package cn.gjxblog.security.browser;


import cn.gjxblog.core.authentication.mobile.MobileAuthenticationSecurityConfig;
import cn.gjxblog.core.captcha.CaptchaSecurityFilter;
import cn.gjxblog.core.captcha.SmsCaptchaSecurityFilter;
import cn.gjxblog.core.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.social.security.SpringSocialConfigurer;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/1/15 16:28
 */
@Configuration
public class BrowserSercurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Bean
    public PasswordEncoder  passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private AuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler myAuthenticationFailedHandler;

    @Autowired
    private MobileAuthenticationSecurityConfig mobileAuthenticationSecurityConfig;

    @Autowired
    private SpringSocialConfigurer springSocialConfigurer;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CaptchaSecurityFilter captchaSecurityFilter = new CaptchaSecurityFilter();
        captchaSecurityFilter.setAuthenticationFailureHandler(myAuthenticationFailedHandler);
        captchaSecurityFilter.setSecurityProperties(securityProperties);
        captchaSecurityFilter.afterPropertiesSet();


        SmsCaptchaSecurityFilter smsCaptchaSecurityFilter = new SmsCaptchaSecurityFilter();
        smsCaptchaSecurityFilter.setAuthenticationFailureHandler(myAuthenticationFailedHandler);
        smsCaptchaSecurityFilter.setSecurityProperties(securityProperties);
        smsCaptchaSecurityFilter.afterPropertiesSet();


        //
        http
            .apply(springSocialConfigurer)//第三方认证过滤器
            .and()
            .addFilterBefore(smsCaptchaSecurityFilter,UsernamePasswordAuthenticationFilter.class)//在用户名密码验证器过滤器前添加验证码认证过滤器
            .addFilterBefore(captchaSecurityFilter,UsernamePasswordAuthenticationFilter.class)//在用户名密码验证器过滤器前添加验证码认证过滤器
            .formLogin()
            .loginPage("/authentication/require")//登录页面&登录请求控制器
            .loginProcessingUrl("/home/login")//自定义请求登录地址
            .successHandler(myAuthenticationSuccessHandler)//自定义登录成功处理器
            .failureHandler(myAuthenticationFailedHandler)//自定义登录失败处理器
            .and()
            .authorizeRequests()
            .antMatchers(securityProperties.getBrowser().getLoginPage(),
                    "/authentication/require",
                    "/login/mobile",
                    "/captcha/*",
                    securityProperties.getBrowser().getRegistPage(),
                    "/social/user",
                    "/home/regist").permitAll()//不需要认证请求配置
            .anyRequest()
            .authenticated()
            .and()
            .csrf().disable()//关闭跨站伪造攻击防护
            .apply(mobileAuthenticationSecurityConfig);//追加其它配置

    }
}
